﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace TIDocLoaderService
{
    public partial class TiDocloaderService : ServiceBase
    {
        public TiDocloaderService()
        {
            InitializeComponent();
        }

        private static bool bNext = false; 
        protected void MyRun()
        {
            do
            {
                FileStream fs = new FileStream(@"c:\Temp\"+Guid .NewGuid ().ToString (),
                FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");
                m_streamWriter.Flush();
                m_streamWriter.Close();
            }
            while (bNext);
        }


        protected override void OnStart(string[] args)
        {
            FileStream fs = new FileStream(@"c:\Temp\mcWindowsService.txt",
            FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");
            m_streamWriter.Flush();
            m_streamWriter.Close();
            bNext = true;
            MyRun();

        }

        protected override void OnStop()
        {
            FileStream fs = new FileStream(@"c:\Temp\mcWindowsService.txt",
            FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n"); m_streamWriter.Flush();
            m_streamWriter.Close();
            bNext = false;
        }
    }
}
